package com.ltx.controller;


import com.ltx.pojo.Card;
import com.ltx.pojo.Coach;
import com.ltx.pojo.Member;
import com.ltx.service.CoachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/Coach")
public class CoachController {

    private final CoachService coachService;
    public CoachController(@Qualifier("coachServiceImpl") CoachService coachService) {
        this.coachService = coachService;
    }
    //查询所有教练信息
    @RequestMapping("/allCoach")
    public String  list(Model model){
        List<Coach> coaches = coachService.queryCoachMapper();
        model.addAttribute("list",coaches);
        for (Coach coach : coaches) {
            System.out.println(coach);
        }
        return "/allCoach";
    }
    //跳转到添加教练页面
    @RequestMapping("/toAddCoach")
    public  String toAddCoach(){
        return ("/addCoach");
    }

    @RequestMapping("/addCoach")
    public  String addCoach(Coach coach){
        coachService.addCoachMapper(coach);
        return ("redirect:/Coach/allCoach");
    }
    //跳转到修改教练页面
    @RequestMapping("/toUpdateCoach")
    public  String   toUpdateCoach(int fitnessId,Model model){
        Coach coach = coachService.queryByCoachMapper(fitnessId);
        model.addAttribute("coach",coach);
        return "/UpdateCoach";
    }
    //修改教练
    @RequestMapping("/UpdateCoach")
    public  String UpdateCoach(Coach coach){
        coachService.updateCoachMapper(coach);
        return ("redirect:/Coach/allCoach");
    }
    //删除教练
    @RequestMapping("deleteCoach")
    public String deleteMember(int fitnessId)
    {
        coachService.deleteCoachMapper(fitnessId);
        return "redirect:/Coach/allCoach";
    }
    //根据名字查询会员
    @RequestMapping("queryCoach")
    public  String queryMember(int fitnessId,Model model){
        Coach coach = coachService.queryByCoachMapper(fitnessId);
        List<Coach> coaches=new ArrayList<>();
        coaches.add(coach);
        if(coach==null){
            coaches=coachService.queryCoachMapper();
            model.addAttribute("error","未查询到");
        }
        model.addAttribute("list",coaches);
        return "allCoach";
    }

}
